package persentation.presentDepartmentDean;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import businesslogic.courseBl.CourseController;
import po.coursePO.CoursePO;
import dataservice.courseDataService.CourseDatabaseService;
import vo.courseVO.CourseVO;

public class ModifyCoursePanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTable table;
	private DepartmentdeanTabbedPane tabbedPane;
	private ArrayList<CourseVO> courseVOs;
	private Vector data, columnName;
	private MyModel model;
	private String depart;
	private ModifyCourse modifyCourse;
	private CourseController cc;
	private static final String ADDRESS_STRING="localhost";

	public ModifyCoursePanel(CourseController c, String department) {
		setSize(900, 690);
		setLayout(null);
		depart = department;
		cc = c;

		columnName = new Vector();
		columnName.add("课程编号");
		columnName.add("课程名称");
		columnName.add("课程性质");
		columnName.add("学分");
		columnName.add("学时");
		columnName.add("院系");
		columnName.add("上课时间");
		columnName.add("上课地点");

		table = new JTable();
		table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		table.setFillsViewportHeight(true);
		JScrollPane jScrollPane = new JScrollPane(table);
		jScrollPane.setBounds(44, 10, 742, 405);
		add(jScrollPane);

		findAllCourse();

		JButton button = new JButton("修改课程信息");
		button.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		button.setBounds(289, 446, 176, 23);
		add(button);

		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (table.getSelectedRow() != -1) {
					CourseVO courseVO = courseVOs.get(table.getSelectedRow());
					System.out.printf("选中的行号是%s\n", table.getSelectedRow());
					System.out.printf("查看的课程名是%s\n", courseVO.getCourseName());
					modifyCourse = new ModifyCourse(cc, courseVO, courseVO
							.getCourseName() + " 修改详细信息", depart);

					tabbedPane.addOne(courseVO.getCourseName() + " 修改详细信息",
							modifyCourse);
					modifyCourse.setTabbedPane(tabbedPane);

				}
			}
		});

		JButton button_1 = new JButton("刷新");
		button_1.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		button_1.setBounds(548, 447, 93, 23);
		add(button_1);
		button_1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				findAllCourse();
			}
		});
	}

	public void setTabbedPane(DepartmentdeanTabbedPane tabbedPane) {
		this.tabbedPane = tabbedPane;
	}

	public void findAllCourse() {
		try {
			CourseDatabaseService u = (CourseDatabaseService) Naming
					.lookup("rmi://"+ADDRESS_STRING+":8888/courseData");
			ArrayList<CoursePO> coursePOs = u.findCourseByDepartment(depart);
			courseVOs = new ArrayList<CourseVO>();
			data = new Vector();
			for (CoursePO c : coursePOs) {
				CourseVO cVO = new CourseVO(c.getCourseId(), c.getCourseName(),
						c.getTeacherID(), c.getTeacherName(), c.getCredit(),
						c.getCoursePeriod(), c.getClassroom(),
						c.getAssistant(), c.getCourseTime(), c.getAttribute(),
						c.getMaxStudent(), c.getTerm(), c.getDepartmentID(),
						c.getBook(), c.getIntroduction());
				courseVOs.add(cVO);
				Vector v = new Vector();
				v.add(c.getCourseId());
				v.add(c.getCourseName());
				v.add(c.getAttribute());
				v.add(c.getCredit());
				v.add(c.getCoursePeriod());
				v.add(c.getDepartmentID());
				v.add(c.getCourseTime());
				v.add(c.getClassroom());
				data.add(v);
			}
			model = new MyModel(data, columnName);
			table.setModel(model);
		} catch (RemoteException e) {
			DepartmentdeanExceptionHandler.networkException();
			System.out.println(e.getCause());
			System.out.println(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
